package com.hzsd.zengbanyi.biaodan_text.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

public class SendErrorLogService extends Service{
	public String exString="";
	public String HOST="";
	public String userId="";
	public String strurl="";
	//http://192.168.2.39:4023/padlog.ashx?flag=list&ApplicationName=&ActionType=&Description=&UserId=&FirstItem=&SecondItem=
	
	private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(getApplicationContext(), "环境监测错误信息已发送服务器！", Toast.LENGTH_LONG).show();
				
				break;

			default:
				break;
			}
		};
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if("hzst.startech.hjjc.service.SendErrorLogService".equals(intent.getAction())){
			//读取发送IP地址和用户userid
			SharedPreferences preferences=getSharedPreferences("user", MODE_PRIVATE);
			HOST=preferences.getString("upLoadurlPath", "");
			userId=preferences.getString("userId", "");
			//读取错误日志信息
			SharedPreferences sp=getSharedPreferences("errorlog", MODE_PRIVATE);
			exString=sp.getString("exString", "");
			
			//String url="http://"+HOST+"/padlog.ashx?flag=list";
			String url="http://"+HOST+"/AppModules/PadInterface/padlog.ashx";
			strurl=url+"&ApplicationName="+""+"&ActionType="+""+"&UserId="+userId+"&FirstItem="+""+"&SecondItem="+""+"&Description="+exString;
			System.out.println("错误日志地址："+strurl);
			//上传错误日志
			if(!"".equals(exString)){
				new Thread(){
					public void run() {
						
						try {
							String data=getJsonData(strurl);
							System.out.println("返回data数据："+data);
							Message msg=Message.obtain();
							msg.obj=data;
							msg.what=1;
							handler.sendMessage(msg);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}.start();
			}
		}
		
		return super.onStartCommand(intent, flags, startId);
	}

	// 访问网络
		public String getJsonData(String urlPath) throws Exception {

			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] data = new byte[1024];

			int len = 0;

			URL url = new URL(urlPath);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream inStream = null;
			try {
				inStream = conn.getInputStream();

			} catch (Exception e) {
				e.printStackTrace();
				if (e instanceof SocketTimeoutException
						|| e instanceof UnknownHostException) {
					System.out.println("请求网络超时：" + urlPath + "~~~");
				}
				return "";
			}
			while ((len = inStream.read(data)) != -1) {
				outStream.write(data, 0, len);

			}
			inStream.close();

			String str = new String(outStream.toByteArray(), "UTF-8");

			return str;
		}

}
